package com.example.demo.exception;

import cn.dev33.satoken.util.SaResult;
import com.example.demo.constant.ResultCode;
import com.example.demo.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.naming.AuthenticationException;
import java.nio.file.AccessDeniedException;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // sa-token全局异常拦截
    @ExceptionHandler
    public SaResult handleException(final Exception ex, final HttpServletRequest request) {
        ex.printStackTrace();
        return SaResult.error(ex.getMessage());
    }

    @ExceptionHandler(PublicException.class)
    @ResponseBody
    public ResponseEntity<Map<String, String>> handlePublicException(PublicException e) {
        Map<String, String> result = new HashMap<>();
        result.put("code", "400"); // 自定义业务码
        result.put("msg", e.getMessage());
        result.put("data", null);

        return ResponseEntity.badRequest().body(result);
    }
//    @ExceptionHandler(BusinessException.class)
//    @ExceptionHandler(TokenException.class)
//    public Result<Void> handleBusinessException(TokenException e) {
//        return Result.error(ResultCode.UNAUTHORIZED);
//    }
//
//
//    @ExceptionHandler(AuthenticationException.class)
//    public Result<Void> handleAuthenticationException(AuthenticationException e) {
//        return Result.error(ResultCode.UNAUTHORIZED);
//    }
//
//    @ExceptionHandler(AccessDeniedException.class)
//    public Result<Void> handleAccessDeniedException(AccessDeniedException e) {
//        return Result.error(ResultCode.FORBIDDEN);
//    }


}
